﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Kardex.Register
{
    public class PresenterRegisterNewKardexView
    {
        IRegisterBasicView viewRegisterBasic;
        Model.Kardex newKardex = new Model.Kardex() { Person = new Model.Person(), Allotment=new List<Model.Allotment>() };
        IRegisterNewKardexProductView view;
        public PresenterRegisterNewKardexView(IRegisterNewKardexProductView View)
        {
            view = View;
            newKardex = new Model.Kardex() { Person = new Model.Person(), Allotment=new List<Model.Allotment>() }; 
        }


        public void GoStepTwo()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newKardex = view.RegisterBasic.KardexProductToRegister;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void RegisterKardex()
        {
            try
            {
                view.RegisterBasic.ChargeDataToBasic();
                newKardex = view.RegisterBasic.KardexProductToRegister;
                if (!PassFirstStep())
                {
                    view.CallFirstPage();
                    ProcedureAfterCall();
                }
                else if (!PassSecondStep())
                {
                    view.CallSecondPage();
                    ProcedureAfterCall();
                }
                else
                {
                    ManagerKardex.NewKardex(newKardex);
                    view.SystemMessage("El Kardex Fue Registrado Exitosamente.!!!!");
                    view.RestartRegisterMedicineProduct();
                }
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Registrar"));
                view.CallSecondPage();
                ProcedureAfterCall();
            }
        }

        private bool PassSecondStep()
        {
            try
            {
                Model.Kardex mp = new Model.Kardex();
                mp.Person = view.RegisterBasic.KardexProductToRegister.Person;
                ManagerKardex.VerifyDataPersonIsCorrect(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message,"Registrar"));
                return false;
            }
        }

        private bool PassFirstStep()
        {

            try
            {
                Model.Kardex mp = new Model.Kardex();
                mp.Allotment = view.RegisterBasic.KardexProductToRegister.Allotment;
                ManagerKardex.VerifyDataPersonIsKardexWithoutPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Registrar"));
                return false;
            }
        }

        public void GoBackPage()
        {
            viewRegisterBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewRegisterBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newKardex = view.RegisterBasic.KardexProductToRegister;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {

            if (viewRegisterBasic != null)
                newKardex = viewRegisterBasic.KardexProductToRegister;
            viewRegisterBasic = view.RegisterBasic;
            viewRegisterBasic.KardexProductToRegister = newKardex;
            viewRegisterBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
